package cn.ges.balance

import java.time.LocalDate
import java.time.LocalDateTime

/**
 * 结算时间计算工具
 * @author gespent@163.com
 * @date 2020/01/10 11:21
 */
enum class BalanceDateCast() {
    // 超过结算时间 往前进一天 反之返回当前
    EXCEED_PLUS_ONE,

    // 超过结算时间 返回当前 反之前推一天
    EARLY_MIN_ONE,

    // 超过结算时间 返回昨天 反之前推两天
    EXCEED_MIN_ONE_EARLY_MIN_TWO;

    companion object {
        fun balanceDateCast(balanceHour: Int, mode: BalanceDateCast): LocalDate {
            val now = LocalDateTime.now()
            val hour = now.hour
            val localDate = now.toLocalDate()
            return when (mode) {
                EXCEED_PLUS_ONE -> {
                    if (hour >= balanceHour) {
                        localDate.plusDays(1)
                    } else {
                        localDate
                    }
                }
                EARLY_MIN_ONE -> {
                    if (hour >= balanceHour) {
                        localDate
                    } else {
                        localDate.minusDays(1)
                    }
                }
                EXCEED_MIN_ONE_EARLY_MIN_TWO -> {
                    if (hour >= balanceHour) {
                        localDate.minusDays(1)
                    } else {
                        localDate.minusDays(2)
                    }
                }
            }
        }
    }
}